<?php 

	require "config.php";
	
	session_start();
	
	$videoid = $_GET['videoid'];
	
	// Mentés
	if(isset($_POST['save-btn'])){
		
		$conn->query("UPDATE videos SET name='$_POST[name]', description='$_POST[description]' WHERE id=$videoid");
		
		header("Location: videos.php");
		
	}
	
	// Indexkép
	if(isset($_POST['upload-btn'])){
		
		$file_name = $_FILES['upload-file']['name'];
		$tmp_name = $_FILES['upload-file']['tmp_name'];
		
		$mappa = getcwd();
		
		$eleresi_ut = $mappa."\\users\\".$_SESSION['username']."\\".$file_name;
		
		if(move_uploaded_file($tmp_name, $eleresi_ut)){
			
			$conn->query("UPDATE videos SET tn_name='$file_name' WHERE id=$videoid");
			
			header("Location: videos.php");
			
		}
		else{
			echo "<script>alert('A kép feltöltése sikertelen!')</script>";
		}
		
	}
	
	// Videó lekrdezése
	$lekerdezes = "SELECT * FROM videos WHERE id=$videoid";
	$talalt_video = $conn->query($lekerdezes);
	$video = $talalt_video->fetch_assoc();
	
	if($_SESSION['id'] != $video['userid']){
		
		header("Location: index.php");
		
	}

?>
<!DOCTYPE html>
<html lang="hu">

	<head>
		
		<!-- TITLE -->
		<title>Videó szerkesztése</title>
		
		<!-- CSS -->
		<link rel="stylesheet" href="css/style.css">
		
	</head>
	<body>
	
		<!-- NAV -->
		<?php require "nav.php"; ?>
		
		<!-- CONTENT -->
		<form method="post" class="reglog">
		
			<label class="form-header">Információ szerkesztése</label>
			
			<input type="text" name="name" placeholder="Videó neve" value="<?= $video['name']; ?>">
			
			<textarea name="description" placeholder="Videó leírása..."><?= $video['description']; ?></textarea>
		
			<input type="submit" name="save-btn" value="Mentés" style="margin-bottom: 0;">
		
		</form>
		
		<!-- INDEXKÉP -->
		<form method="post" class="reglog" enctype="multipart/form-data">
		
			<label class="form-header">Indexkép feltöltése</label>
		
			<input type="file" name="upload-file">
			
			<input type="submit" name="upload-btn" value="Feltöltés" style="margin-bottom: 0;">
		
		</form>
	
	</body>
	
	<?php require "footer.php"; ?>
	
</html>